//index.js
//获取应用实例
const app = getApp()
var util= require("../../utils/libs.js");
var api = require("../../utils/api.js");
Page({
  data: {
    dataList:[],
    avatar:[],
    uid:"",
    keywrod: ''
  },
  inputChange(event){
    let that = this 
    let values = event.detail.value
    that.setData({
      keywrod:values
    })
  },
  onKeywordConfirm(){
    let that =this
    util.request(api.allUser,{
      page:"0",
      limit:"10",
      key:that.data.keywrod,
    },"GET")
    .then(function (res) {
      if(res.error_code === 0){
        let result = that.getstasusText(res.data.list.data)
        if(result.length>0){
          that.setData({
            dataList:result
          }) 
        }else{
          that.setData({
            dataList:result
          }) 
          wx.showToast({
            title:"搜索的内容不存在",
            icon: 'errno',
            duration: 2000
          })
        }
      }
    })
  },
  submitPlease (e) {
    let that = this 
    let id = e.target.dataset.index
    util.request(api.addInvite,{
      uid:id,
      id:that.data.id
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        wx.showToast({
          title:"邀请成功",
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  onLoad(options){
    let that =this
    let uid= wx.getStorageSync('uid')
    that.setData({
      id:options.id
    })
    that.getinviteList()
  },
  getinviteList(){
    let that =this
    util.request(api.allUser,{
      page:"0",
      limit:"10"
    },"GET")
    .then(function (res) {
      if(res.error_code === 0){
        let result = that.getstasusText(res.data.list.data)
        that.setData({
          dataList:result
        })
      }
    })
  },
  getstasusText(arr){
   arr.forEach(val => {
     if(val.status == null){
       val.state_text = "邀请"
     }else{
      val.state_text = "已邀请"
     }
   });
   return arr
  }
})
